Skip to content

fix: end when quit() or disconnect() is called while reconnecting - #2245

Merged
PavelPashov merged 1 commit into
redis:mainfrom
bercedev:fix/disconnect-while-reconnecting
Oct 23, 2026
Merged

PavelPashov merged 1 commit into
redis:mainfrom
bercedev:fix/disconnect-while-reconnecting

Conversation

@bercedev

@bercedev bercedev commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

the standalone Redis side of #2240.

after a dropped connection the client is reconnecting with the retry timer armed. disconnect() clears the timer, but since the status isn't wait it calls connector.disconnect() on a socket that already closed, so close never comes again, closeHandler never runs and end is never emitted. the client stays reconnecting for good. quit() takes the same path (it calls disconnect() when the stream isn't writable).

this hangs BullMQ's Worker.close(), which awaits end on its blocking connection: snapotter-hq/SnapOtter#3604 has the full trace, from an event loop stall that trips BullMQ's watchdog into disconnect(true) to close() hanging in afterAll.

in reconnecting with reconnect === false, disconnect() now ends the client the way closeHandler would for a manual close (status end, queues flushed) instead of waiting for a close that won't come. it doesn't run closeHandler again, because that would emit a second close for the same socket. disconnect(true) while reconnecting is unchanged.

tests in test/functional/disconnection.ts, like the cluster ones: disconnect() and quit() on the reconnecting event end the client and later commands reject with Connection is closed.. both fail on main (expected 'reconnecting' to equal 'end'). ran unit + functional against redis 7.0.15: 2333 passing, the 7 failures (zrank/zrevrank WITHSCORE, scanStream) fail the same on main with that redis version. tsc and prettier are clean.


Note

Medium Risk
Touches core connection teardown in disconnect(); behavior change is narrow (manual close during reconnect) but shutdown paths are easy to get wrong.

Overview
Fixes a hang where disconnect() or quit() during reconnecting left the client stuck in that state and never emitted end.

After a drop, the socket is already closed and close has run, but a retry timer is still armed. A manual disconnect(false) cleared the timer and called connector.disconnect() on a dead socket, so closeHandler never ran again. The change adds a reconnecting && !reconnect path that finishes the shutdown inline: status end, command queues flushed with Connection is closed., without re-invoking closeHandler (avoids a duplicate close). disconnect(true) while reconnecting is unchanged.

Functional tests cover disconnect() and quit() in that window and assert later commands reject.

Reviewed by Cursor Bugbot for commit 1cd3eb9. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-22T18:47:27.026215Z
We couldn’t complete this review. Comment @codex review to try again.
1cd3eb9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

snapotter-hq added a commit to snapotter-hq/SnapOtter that referenced this pull request Oct 23, 2026
… grids via Sharp (#3604)

Calling disconnect(false) on an ioredis client in the "reconnecting" state
cancels reconnectTimeout and invokes connector.disconnect() on the already
closed socket from the prior connection, so "end" never fires and BullMQ's
Worker.close() hangs in afterAll after a >6s event-loop stall trips the
waitForJob watchdog.

Patch Redis.prototype.disconnect in connection.ts (matching upstream
redis/ioredis#2245) to transition reconnecting clients to "end" and flush
both active and saved command queues when reconnect is false. In
qr-generate.ts, rasterize the QR module matrix at 1 pixel per module and
upscale with Sharp nearest-neighbor interpolation instead of allocating a
400 MB RGBA buffer in pngjs at size 10000.

Closes #3604
snapotter-hq added a commit to snapotter-hq/SnapOtter that referenced this pull request Oct 23, 2026
… grids via Sharp (#3604) (#3730)

Calling disconnect(false) on an ioredis client in the "reconnecting" state cancels reconnectTimeout and invokes connector.disconnect() on the already-closed socket from the prior connection, so "end" never fires and BullMQ's Worker.close() hangs in afterAll after a >6s event-loop stall trips the waitForJob watchdog.

Patch Redis.prototype.disconnect in connection.ts (matching upstream redis/ioredis#2245) to transition reconnecting clients to "end" and flush offlineQueue, commandQueue, and prevCommandQueue when reconnect is false. In qr-generate.ts, rasterize the QR module matrix at 1 pixel per module and upscale with Sharp nearest-neighbor interpolation instead of allocating a 400 MB RGBA buffer in pngjs at size 10000.

Closes #3604

@PavelPashov PavelPashov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bercedev LGTM, thank you for the contribution

@PavelPashov
PavelPashov merged commit 81bc16a into redis:main Oct 23, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants